package com.deepexp.servicetest;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.deepexp.servicetest.net.ApiService;
import com.deepexp.servicetest.net.SSLSocketClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Author xyh
 * create time 2021/2/4
 */
public class RequestUtils {
    private static RequestUtils sNewInstance = null;

    private Context mContext;
    private static Retrofit retrofit;
    private ApiService apiService;

    public ApiService getApiService() {
        return apiService;
    }

    public void setApiService(ApiService apiService) {
        this.apiService = apiService;
    }

    public static RequestUtils getInstance(Context context) {
        if (sNewInstance == null) {
            synchronized (RequestUtils.class) {
                sNewInstance = new RequestUtils(context);
            }
        }
        return sNewInstance;
    }

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

        @Override
        public void log(String message) {
//                if (Constants.TEST_MODE) {
            Log.d("OKHttp", message);
//                }
        }
    });

    public RequestUtils(Context mContext) {
        this.mContext = mContext;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(logging);
        if (Build.VERSION.SDK_INT < 29) {
            builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory());
        }
        OkHttpClient httpClient = builder.build();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:s").create();
        retrofit = new Retrofit.Builder().baseUrl("http://192.168.0.73:8080")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(httpClient).build();
//        retrofit.create(ParkService.class);
        apiService = retrofit.create(ApiService.class);
    }


}
